package trading.ctm.yunjing.module.login.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import cc.vv.baselibrary.activity.BaseActivity
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.lkbasecomponent.util.LKSPUtil
import kotlinx.android.synthetic.main.activity_login.*
import trading.ctm.yunjing.R
import trading.ctm.yunjing.module.main.activity.HomeActivity
import trading.ctm.yunjing.util.acountChange
import trading.ctm.yunjing.util.isAccount
import trading.ctm.yunjing.util.isPassword
import trading.ctm.yunjing.util.passwordChange

/**
 * @desc 登录
 * @author zouxiaorui
 * @date  2018/8/8
 */
class LoginActivity : BaseActivity() {

    private val REMEMBER_ACCOUNT = "REMEMBER_ACCOUNT"
    private var mAccount = ""
    private var mPassword = ""

    override fun initLayoutRes(): Int {
        return R.layout.activity_login
    }

    override fun initView(bundle: Bundle?) {
        super.initView(bundle)

        //获取本地缓存的账号
        mAccount = LKSPUtil.getInstance().getString(REMEMBER_ACCOUNT, "")
        //设置账号
        tiet_al_account?.setText(mAccount)
        if(isAccount(mAccount)){
            iv_al_remember_account.visibility = View.VISIBLE
        }else{
            iv_al_remember_account.visibility = View.GONE
        }
        //光标设置在账号尾部
        tiet_al_account?.setSelection(mAccount?.length)
    }

    override fun initAction(data: Bundle?) {
        super.initAction(data)

        btbv_al_title.setEventInterface(object : BaseTopBarView.EventInterface{
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {
            }

            override fun rightOnClick() {
            }

        })
        //登录
        tv_al_login.setOnClickListener {
            mAccount = tiet_al_account.text.toString()
            mPassword = tiet_al_password.text.toString()
            if(isAccount(mAccount) && isPassword(mPassword)) {
                passwordLogin()
            }else if(!isAccount(mAccount)){
                LKToastUtil.showToastShort("请输入正确账号")
            }else if(!isPassword(mPassword)){
                LKToastUtil.showToastShort("密码格式不正确")
            }
        }



        //注册
        tv_al_regist.setOnClickListener {
            startActivity(Intent(this@LoginActivity, RegistActivity::class.java))
        }

        //忘记密码
        tv_al_forget_password.setOnClickListener {
            startActivity(Intent(this@LoginActivity, ForgetActivity::class.java))
        }

        //账号监听
        acountChange(tiet_al_account, tiet_al_password, iv_al_remember_account, tv_al_login)
        passwordChange(tiet_al_password, tiet_al_account, iv_al_remember_account, tv_al_login)


    }

    /**
     * 账号密码登录
     */
    private fun passwordLogin() {
        LKSPUtil.getInstance().put(REMEMBER_ACCOUNT, mAccount)
        startActivity(Intent(this@LoginActivity, HomeActivity::class.java))
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (isShouldHideInput(v, ev)) {
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm?.hideSoftInputFromWindow(v!!.windowToken, 0)
                iv_al_logo.visibility = View.VISIBLE
            }
            return super.dispatchTouchEvent(ev)
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    /**
     * 点击区域是否在文本区
     * @param v
     * @param event
     * @return
     */
    private fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val leftTop = intArrayOf(0, 0)
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop)
            val left = leftTop[0]
            val top = leftTop[1]
            val bottom = top + v.height
            val right = left + v.width
            return !(event.x > left && event.x < right
                    && event.y > top && event.y < bottom)
        }
        return false
    }

}